﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using IRUM.Core;
using IRUM.XingAPI;
using IRUM.Utils;
using System.Threading;

namespace IRUM.TR
{
    /// <summary>
    /// 시간대별 프로그램 매매추이
    /// </summary>
    public class t1632 : CBaseQuery
    {
        public event ProgramTradeDataHandler OnReceived;
        private List<ProgramTradeData> m_list = new List<ProgramTradeData>();

        private string kind = string.Empty;
        private string cts_date = " ";
        private string cts_time = " ";

        /// <summary>
        /// 시간대별 프로그램 매매추이
        /// <param name="kind">시장구분, K:코스피, D:코스닥</param>
        /// </summary>
        public t1632(string kind)
        {
            this.kind = kind;

            SetClassData(TR_Type.t1632);
        }

        public void SetOutBlockData(int Index)
        {
            ProgramTradeData pt = new ProgramTradeData();

            pt.kind = kind;
            pt.gubun = "M";
            pt.date = IRUM_Utils.GetLastTradingDateForDay(DateTime.Now).ToString("yyyyMMdd");    //마지막 거래일 가져와서 처리해야 함.
            pt.time = m_Query.GetFieldData(m_OutBlock1Name, "time", Index);
            pt.jisu = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "k200jisu", Index));
            pt.change = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "change", Index));
            pt.totalSum = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "tot3", Index));
            pt.totalBuy = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "tot1", Index));
            pt.totalSell = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "tot2", Index));
            pt.chaSum = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "cha3", Index));
            pt.chaBuy = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "cha1", Index));
            pt.chaSell = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "cha2", Index));
            pt.bichaSum = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "bcha3", Index));
            pt.bichaBuy = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "bcha1", Index));
            pt.bichaSell = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "bcha2", Index));
            pt.volume = 0;

            int sign = Convert.ToInt32(m_Query.GetFieldData(m_OutBlock1Name, "sign", Index));
            if (sign > 3)
            {
                pt.change = pt.change * -1;
            }

            m_list.Add(pt);
        }

        public override void HandleQuery()
        {
            int OccursCount = m_Query.GetBlockCount(m_OutBlock1Name);

            for (int OccursIndex = 0; OccursIndex < OccursCount; OccursIndex++)
            {
                try
                {
                    SetOutBlockData(OccursIndex);
                }
                catch (Exception e)
                {
                    Debug.Print(e.ToString());
                    continue;
                }
            }

            //연속조회
            if (m_Query.IsNext == true && OccursCount > 0)
            {
                Thread.Sleep(500);
                cts_date = m_Query.GetFieldData(m_OutBlockName, "date", 0);
                cts_time = m_Query.GetFieldData(m_OutBlockName, "time", 0);
                RequestNext();
            }
            else
            {
                // 이벤트 발생
                if (OnReceived != null)
                {
                    OnReceived(this, new ProgramTradeDataArgs(m_list));
                }
            }
        }

        protected override void SetRequestData()
        {
            string market = string.Empty;

            switch (kind)
            {
                case "K":           //코스피
                    market = "0";
                    break;
                case "D":           //코스닥
                    market = "1";
                    break;
            }

            m_Query.SetFieldData(m_InBlockName, "gubun", 0, market);    //시장구분, 1, 0:코스피, 1:코스닥
            m_Query.SetFieldData(m_InBlockName, "gubun1", 0, "0");      //금액수량구분, 1, 1:수량, 0:금액
            m_Query.SetFieldData(m_InBlockName, "gubun2", 0, "0");      //직전대비증감구분, 1, 0:Default, 1:직전대비증감
            m_Query.SetFieldData(m_InBlockName, "gubun3", 0, "0");      //전일구분, 1, 1:전일분
            m_Query.SetFieldData(m_InBlockName, "date", 0, cts_date);   //연속일자, 8, 처음조회시 space, 연속조회시 outblock의 date
            m_Query.SetFieldData(m_InBlockName, "time", 0, cts_time);   //연속시간, 6, 처음조회시 space, 연속조회시 outblock의 time
        }
    }
}
